package com.zhuyi.broadcast.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuyi.broadcast.mapper.ProgramElementRelationMapper;
import com.zhuyi.broadcast.mapper.ProgramMapper;
import com.zhuyi.broadcast.mapper.ScheduleProgramRelationMapper;
import com.zhuyi.broadcast.pojo.domain.*;
import com.zhuyi.broadcast.service.DeviceService;
import com.zhuyi.broadcast.service.ElementService;
import com.zhuyi.broadcast.service.ProgramService;
import com.zhuyi.broadcast.service.ScheduleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  节目实现类
 * </p>
 *
 * @author dyj
 * @since 2024-12-12
 */

@Slf4j
@Service
public class ProgramServiceImpl extends ServiceImpl<ProgramMapper, Program> implements ProgramService {

    @Autowired
    private ScheduleProgramRelationMapper scheduleProgramRelationMapper;

    @Autowired
    private ProgramElementRelationMapper programElementRelationMapper;

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private ElementService elementService;

    /**
     * 查询节目列表
     */
    public List<Program> list(Program program) {
        LambdaQueryWrapper<Program> wrapper = new LambdaQueryWrapper<>();
        // 如果 program 中的属性为空，查询所有数据
        return baseMapper.selectList(wrapper);
    }

    /**
     * 删除节目
     * @param programId
     * @return
     */
    @Override
    public boolean removeProgram(Long programId) {
        // 检查节目是否与轮播计划有关联
        if(scheduleProgramRelationMapper.selectCount(
                new LambdaQueryWrapper<ScheduleProgramRelation>()
                        .eq(ScheduleProgramRelation::getProgramId,programId))>0){
            System.out.println("该节目正在被轮播计划使用，无法删除");
            return false;
        }

        //删除节目及其附庸
        delete_pro(programId);

        return true;
    }

    /**
     * 发送节目
     * @param programId
     * @param deviceId
     * @return
     */
    public boolean sendProgram(Long programId, Long deviceId) {
        Schedule schedule = new Schedule();
        //新建一个轮播计划
        scheduleService.save(schedule);
        //将节目添加至轮播计划
        scheduleService.addProgramToSchedule(programId,schedule.getId());
        //将该轮播计划添加至设备
        deviceService.addScheduleToDevice(deviceId,schedule.getId());
        return true;
    }


    /**
     * 删除节目及其附庸
     * @param programId
     * @return
     */
    public boolean delete_pro(Long programId) {

        List<ProgramElementRelation> elements = programElementRelationMapper.selectList(
                new LambdaQueryWrapper<ProgramElementRelation>()
                        .eq(ProgramElementRelation::getProgramId, programId));

        for (ProgramElementRelation element : elements) {
            Long elementId = element.getElementId();
            // 删除节目关联的元素
            elementService.delete_ele(elementId);
            //删除节目和元素的关联
            programElementRelationMapper.delete(
                    new LambdaQueryWrapper<ProgramElementRelation>()
                            .eq(ProgramElementRelation::getProgramId, programId)
            );
        }


        //删除节目
        this.baseMapper.deleteById(programId);

        return true;
    }
}
